package QQ;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class QQClient extends JFrame implements ActionListener{

	
	// 由于本人不会SWING，代码写的烂勿喷 o(∩_∩)o 。
	
	private JTextField[] tf = new JTextField[2];
	private String[] labelStr = { "qq号码：" ,"qq密码：","验证码：" };
	private JLabel[] lb = new JLabel[4];
	private JPasswordField pf;
	private JButton[] bt = new JButton[2];
	private QQKJava3 qq = new QQKJava3();
	private Socket s;
	
	public QQClient() {
		setLayout(null);
		setTitle("手机QQ协议       By ╱―★神★―╲");
		setSize(400, 250);
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		for(int  i = 0 ; i < 3 ; i++) {
			lb[i] = new JLabel(labelStr[i]);
			lb[i].setBounds(32, 32+30 * i, 80, 24);
			add(lb[i]);
		}
		
		for(int  i = 0 ; i < 2 ; i++) {
			tf[i] = new JTextField();
			tf[i].setBounds(120, 32 + 60 * i, 150, 24);
			add(tf[i]);
		}
		
		
		lb[3] = new JLabel("用来显示验证码");
		lb[3].setBounds(300, 92, 100, 40);
		add(lb[3]);
		
		pf =  new JPasswordField();
		pf.setBounds(120, 62, 150, 24);
		add(pf);
	
		
		bt[0] = new JButton("登　录");
		bt[0].setBounds(32, 132, 80, 24);
		add(bt[0]);
		
		bt[1] = new JButton("带验证码登录");
		bt[1].setBounds(120, 132, 150, 24);
		add(bt[1]);
		
		bt[0].addActionListener(this);
		bt[1].addActionListener(this);
		
		try {
			// 连接QQ服务器
			s = new Socket(InetAddress.getByName(QQKJava3.IP), QQKJava3.PORT);
		} catch (Exception e) {
			e.printStackTrace();
			msgbox("连接腾讯服务器失败");
		} 
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == bt[0]) { //登录
			
			try {
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
						s.getOutputStream(), "UTF-8"));
				bw.write(qq.login(tf[0].getText(),pf.getText()));
				bw.flush();
				BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(),"UTF-8"));
				//0=密码错误。1=登录成功，2=需要验证码。（qq.verifyCode()可取验证码图片PNG格式）
				int result = qq.loginReturn(br.readLine());
				switch (result) {
				case 0:
					msgbox("密码错误");
					break;
				case 1:
					msgbox("登录成功");
					break;
				case 2:
					msgbox("需验证码");
					lb[3].setIcon(new ImageIcon(qq.verifyCode()));
					break;
				}
			} catch (Exception ex) {
				ex.printStackTrace();
				msgbox("登录失败");
			} 
			return ;
		}
		
		if(e.getSource() == bt[1]) { //带验证码登录
			try {
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
						s.getOutputStream(), "UTF-8"));
				bw.write(qq.loginAndVC(tf[0].getText(),tf[1].getText()));
				bw.flush();
				BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(),"UTF-8"));
				//0=密码错误。1=登录成功，2=需要验证码。（qq.verifyCode()可取验证码图片PNG格式）
				int result = qq.loginReturn(br.readLine());
				switch (result) {
				case 0:
					msgbox("密码错误");
					break;
				case 1:
					msgbox("登录成功");
					break;
				case 2:
					msgbox("需验证码");
					lb[3].setIcon(new ImageIcon(qq.verifyCode()));
					break;
				}
			} catch (Exception ex) {
				ex.printStackTrace();
				msgbox("登录失败");
			} 

			
			return ;
		}
		
	}
	
	
	private void msgbox(String x){
		JOptionPane.showMessageDialog(null,x);
	}
	
	public static void main(String[] args) {
		
		new QQClient().setVisible(true);
		
	}


}
